/**
 * MemberServiceImpl.java 下午3:53:52 2012-2-29
 * Cyber Information Technology Co.,Ltd.
 *   
 *   http://www.citchina.cn
 *   citchina.org@gmail.com
 */
package cn.cit.wcif.member.biz;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import cn.cit.wcif.member.api.IMember;
import cn.cit.wcif.member.api.IMemberMutable;
import cn.cit.wcif.member.api.IMemberService;
import cn.cit.wcif.member.api.exceptions.EmailExistenceException;
import cn.cit.wcif.member.api.exceptions.KeyNotUniqueException;
import cn.cit.wcif.member.api.exceptions.MemberNonExistenceException;
import cn.cit.wcif.member.api.exceptions.MemberRegistException;
import cn.cit.wcif.member.api.exceptions.NameExistenceException;
import cn.cit.wcif.member.api.exceptions.ObjectNonExistenceException;
import cn.cit.wcif.member.api.persistence.IMemberEntity;
import cn.cit.wcif.member.api.persistence.IMemberPersistenceService;

/**
 * @author Jonathan
 * @version 1.0
 * 
 */
@Service
@Transactional
public class MemberServiceImpl implements IMemberService {

	@Autowired(required = false)
	@Qualifier("memberPersistenceServiceImpl")
	private IMemberPersistenceService memberPersistenceService;

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.cit.outman.member.api.MemberService#createMember()
	 */
	@Override
	public IMemberMutable createMember() {
		return new MemberMutableImpl();
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.cit.outman.member.api.MemberService#saveMember()
	 */
	@Override
	public IMember saveMember(IMember member) {

		MemberMutableImpl result = null;
		IMemberEntity entity = memberPersistenceService.persistMember(member);
		if (null != entity) {
			result = new MemberMutableImpl();
			result.setId(entity.getId());
			result.setName(entity.getName());
		}
		return result;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see cn.cit.outman.member.api.MemberService#login(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public IMember login(String email, String password)
			throws MemberNonExistenceException {
		IMemberEntity entity = null;
		try {
			entity = memberPersistenceService.findMemberByIdentity(email,
					password);
			if (null == entity) {
				throw new MemberNonExistenceException();
			}
			MemberMutableImpl member = new MemberMutableImpl();
			member = translate(entity, member);
			return member;
		} catch (ObjectNonExistenceException e) {
			throw new MemberNonExistenceException(e.getMessage());
		}
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.cit.outman.member.api.MemberService#registMember(java.lang.String,
	 * java.lang.String, java.lang.String)
	 */
	@Override
	public IMember registMember(String email, String password, String name)
			throws KeyNotUniqueException, MemberRegistException {
		IMemberEntity entity = null;
		try {
			entity = memberPersistenceService.saveMember(email, password, name);
		} catch (EmailExistenceException e) {
			throw new KeyNotUniqueException("Existing the Member(email:"
					+ email + ")!");
		} catch (NameExistenceException e) {
			throw new KeyNotUniqueException("Existing the Member(email:"
					+ email + ",name:" + name + ")!");
		} catch (Exception e) {
			e.printStackTrace();
			throw new MemberRegistException(
					"Member regist exception : the Member(email:" + email
							+ ",name:" + name + ")!");
		}
		if (null == entity) {
			return null;
		} else {
			MemberMutableImpl member = new MemberMutableImpl();
			member = translate(entity, member);
			return member;
		}
	}

	/**
	 * @param entity
	 * @param member
	 * @return
	 */
	private MemberMutableImpl translate(IMemberEntity entity,
			MemberMutableImpl member) {
		member.setId(entity.getId());

		member.setEmail(entity.getEmail());// Email,做为唯一登录账号

		member.setPassword(entity.getPassword());// 密码

		member.setName(entity.getName());// 昵称

		member.setSex(entity.getSex());// 性别

		member.setCity(entity.getCity());// 城市

		member.setCellPhone(entity.getCellPhone());// 手机

		member.setSignature(entity.getSignature());// 签名

		member.setAvatar(entity.getAvatar());// 获取头像

		member.setGetingInfoByEmail(entity.isGetingInfoByEmail());// 邮箱是否接受本站点信息

		member.setPromptingForResponseedMyArticle(entity
				.isPromptingForResponseedMyArticle());// 文章有回复信息是否提示
		return member;
	}

	/*
	 * (non-Javadoc)
	 * 
	 * @see
	 * cn.cit.outman.member.api.MemberService#registMember(java.lang.String,
	 * java.lang.String)
	 */
	@Override
	public IMember registMember(String email, String password)
			throws KeyNotUniqueException, MemberRegistException {
		IMemberEntity entity = null;
		try {
			entity = memberPersistenceService.saveMember(email, password);
		} catch (EmailExistenceException e) {
			throw new KeyNotUniqueException("Existing the Member(email:"
					+ email + ")!");
		} catch (Exception e) {
			throw new MemberRegistException(
					"Member regist exception : the Member(email:" + email
							+ ")!");
		}
		if (null == entity) {
			return null;
		} else {
			MemberMutableImpl member = new MemberMutableImpl();
			member = translate(entity, member);
			return member;
		}
	}

}
